<template>
  <div class="electricity-container">
    <!-- Electric Energy Storage -->
    <div class="storage-section">
        <button class="distribution-btn emission-submit-btn" @click="submitAllDeviceParams">
          <span class="btn-text">提交设备参数</span>

        </button>
      <h2 class="title">电储能</h2>
      <div class="info-grid">
        <div class="info-card">
          <label class="info-label">循环老化系数α </label>
          <div class="info-content">
            <input type="number" v-model="electricStorage1" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">循环老化系数β  </label>
          <div class="info-content">
            <input type="number" v-model="electricStorage2" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">循环老化系数γ  </label>
          <div class="info-content">
            <input type="number" v-model="electricStorage3" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">循环老化系数δ  </label>
          <div class="info-content">
            <input type="number" v-model="electricStorage4" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">循环老化系数ε </label>
          <div class="info-content">
            <input type="number" v-model="electricStorage5" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">循环老化系数τ</label>
          <div class="info-content">
            <input type="number" v-model="electricStorage6" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">基于Arrhenius因子的日历老化系数</label>
          <div class="info-content">
            <input type="number" v-model="electricStorage7" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">基于Arrhenius因子的日历老化系数 </label>
          <div class="info-content">
            <input type="number" v-model="electricStorage8" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">基于Arrhenius因子的日历老化系数 </label>
          <div class="info-content">
            <input type="number" v-model="electricStorage9" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">平均温度</label>
          <div class="info-content">
            <input type="number" v-model="electricStorage10" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最大规划容量(MWh)</label>
          <div class="info-content">
            <input type="number" v-model="electricStorage11" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

      </div>
    </div>

    <div class="storage-section">
      <h2 class="title">热储能</h2>
      <div class="info-grid">
        <div class="info-card">
          <label class="info-label">退化因子</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage1" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">自放能损失</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage2" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">静态损耗系数</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage3" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最高运行温度 </label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage4" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最低运行温度  </label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage5" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最大规划容量(MWh)</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage6" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>
      </div>
    </div>

    <div class="storage-section">
      <h2 class="title">冷储能</h2>
      <div class="info-grid">
        <div class="info-card">
          <label class="info-label">退化因子 </label>
          <div class="info-content">
            <input type="number" v-model="coldStorage1" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">自放能损失</label>
          <div class="info-content">
            <input type="number" v-model="coldStorage2" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">静态损耗系数</label>
          <div class="info-content">
            <input type="number" v-model="coldStorage3" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最高运行温度</label>
          <div class="info-content">
            <input type="number" v-model="coldStorage4" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最低运行温度</label>
          <div class="info-content">
            <input type="number" v-model="coldStorage5" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最大规划容量(MWh)</label>
          <div class="info-content">
            <input type="number" v-model="coldStorage6" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>
      </div>
    </div>

  <div class="storage-section">
      <h2 class="title">氢储能</h2>
      <div class="info-grid">
        <div class="info-card">
          <label class="info-label">退化因子</label>
          <div class="info-content">
            <input type="number" v-model="hydrogenStorage1" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最大规划容量(MWh)</label>
          <div class="info-content">
            <input type="number" v-model="hydrogenStorage2" class="form-input small">
            <span class="info-value"></span>
          </div>
        </div>

    </div>
</div>

</div>

</template>

<script lang="ts" setup>
import { ref, onMounted, type Ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const submitAllDeviceParams = async () => {
  try {
    const payloads = [
  { id: 1, value: parseFloat(electricStorage1.value) || 0, url: 'diancn' },
  { id: 2, value: parseFloat(electricStorage2.value) || 0, url: 'diancn' },
  { id: 3, value: parseFloat(electricStorage3.value) || 0, url: 'diancn' },
  { id: 4, value: parseFloat(electricStorage4.value) || 0, url: 'diancn' },
  { id: 1, value: parseFloat(electricStorage5.value) || 0, url: 'diancn' },
  { id: 2, value: parseFloat(electricStorage6.value) || 0, url: 'diancn' },
  { id: 3, value: parseFloat(electricStorage7.value) || 0, url: 'diancn' },
  { id: 4, value: parseFloat(electricStorage8.value) || 0, url: 'diancn' },
  { id: 1, value: parseFloat(electricStorage9.value) || 0, url: 'diancn' },
  { id: 2, value: parseFloat(electricStorage10.value) || 0, url: 'diancn' },
  { id: 3, value: parseFloat(electricStorage11.value) || 0, url: 'diancn' },
  { id: 4, value: parseFloat(thermalStorage1.value) || 0, url: 'recn' },
  { id: 1, value: parseFloat(thermalStorage2.value) || 0, url: 'recn' },
  { id: 2, value: parseFloat(thermalStorage3.value) || 0, url: 'recn' },
  { id: 3, value: parseFloat(thermalStorage4.value) || 0, url: 'recn' },
  { id: 4, value: parseFloat(thermalStorage5.value) || 0, url: 'recn' },
  { id: 1, value: parseFloat(thermalStorage6.value) || 0, url: 'recn' },
  { id: 2, value: parseFloat(coldStorage1.value) || 0, url: 'lengcn' },
  { id: 3, value: parseFloat(coldStorage2.value) || 0, url: 'lengcn' },
  { id: 4, value: parseFloat(coldStorage3.value) || 0, url: 'lengcn' },
  { id: 2, value: parseFloat(coldStorage4.value) || 0, url: 'lengcn' },
  { id: 3, value: parseFloat(coldStorage5.value) || 0, url: 'lengcn' },
  { id: 4, value: parseFloat(coldStorage6.value) || 0, url: 'lengcn' },
  { id: 2, value: parseFloat(hydrogenStorage1.value) || 0, url: 'qingcn' },
  { id: 3, value: parseFloat(hydrogenStorage2.value) || 0, url: 'qingcn' },

    ]

    for (const item of payloads) {
      await axios.put(`http://localhost:8080/${item.url}`, {
        id: item.id,
        value: item.value
      })
    }

    ElMessage.success('所有设备参数提交成功')
  } catch (err) {
    ElMessage.error('提交失败，请检查网络或数据')
    console.error(err)
  }
}

const fetchAndSet = async (url: string, variable: Ref<string>) => {
  try {
    const response = await axios.get(url);
    variable.value = response.data.value;
  } catch {
    ElMessage.error(`加载 ${url} 失败`);
  }
};

onMounted(async () => {

    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage1);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage2);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage3);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage4);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage5);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage6);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage7);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage8);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage9);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage10);
    await fetchAndSet('http://localhost:8080/diancn/9', electricStorage11);

    await fetchAndSet('http://localhost:8080/recn/9', thermalStorage1);
    await fetchAndSet('http://localhost:8080/recn/9', thermalStorage2);
    await fetchAndSet('http://localhost:8080/recn/9', thermalStorage3);
    await fetchAndSet('http://localhost:8080/recn/9', thermalStorage4);
    await fetchAndSet('http://localhost:8080/recn/9', thermalStorage5);
    await fetchAndSet('http://localhost:8080/recn/9', thermalStorage6);

    await fetchAndSet('http://localhost:8080/lengcn/9', coldStorage1);
    await fetchAndSet('http://localhost:8080/lengcn/9', coldStorage2);
    await fetchAndSet('http://localhost:8080/lengcn/9', coldStorage3);
    await fetchAndSet('http://localhost:8080/lengcn/9', coldStorage4);
    await fetchAndSet('http://localhost:8080/lengcn/9', coldStorage5);
    await fetchAndSet('http://localhost:8080/lengcn/9', coldStorage6);

    await fetchAndSet('http://localhost:8080/qingcn/9', hydrogenStorage1);
    await fetchAndSet('http://localhost:8080/qingcn/9', hydrogenStorage2);
  // 修改为你的真实接口
})


// Hydrogen Storage
    const electricStorage1 = ref('')
    const electricStorage2 = ref('')
    const electricStorage3 = ref('')
    const electricStorage4 = ref('')
    const electricStorage5 = ref('')
    const electricStorage6 = ref('')
    const electricStorage7 = ref('')
    const electricStorage8 = ref('')
    const electricStorage9 = ref('')
    const electricStorage10 = ref('')
    const electricStorage11 = ref('')

// Hydrogen Storage
    const thermalStorage1 = ref('')
    const thermalStorage2 = ref('')
    const thermalStorage3 = ref('')
    const thermalStorage4 = ref('')
    const thermalStorage5 = ref('')
    const thermalStorage6 = ref('')

// Hydrogen Storage
    const coldStorage1 = ref('')
    const coldStorage2 = ref('')
    const coldStorage3 = ref('')
    const coldStorage4 = ref('')
    const coldStorage5 = ref('')
    const coldStorage6 = ref('')

    const hydrogenStorage1 = ref('')
    const hydrogenStorage2 = ref('')
</script>

<style scoped>
.submit-emission-container {
  text-align: center;
  margin-top: 24px;
}

.emission-submit-btn {
  background-color: #7c3aed; /* 绿色 */
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 4px 10px rgba(22, 163, 74, 0.2);
}

.emission-submit-btn:hover {
  background-color: #7c3aed;
  box-shadow: 0 6px 16px rgba(22, 163, 74, 0.3);
}
.electricity-container {
  background-color: #f8fafc;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 36px;
  max-width: 920px;
  margin: 0 auto;
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  color: #1e293b;
}

.title {
  color: #4c1d95;
  text-align: center;
  margin-bottom: 32px;
  font-size: 28px;
  font-weight: 700;
  padding-bottom: 16px;
  border-bottom: 2px solid #7c3aed;
  letter-spacing: 0.5px;
}

.storage-section {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 28px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 18px;
}

.info-card {
  background-color: #f8fafc;
  padding: 18px;
  border-radius: 10px;
  border-left: 4px solid #7c3aed;
  transition: transform 0.2s, box-shadow 0.2s;
}

.info-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.info-label {
  display: block;
  font-weight: 500;
  color: #475569;
  margin-bottom: 10px;
  font-size: 15px;
}

.info-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.form-input {
  padding: 10px 16px;
  border: 1px solid #053676;
  border-radius: 6px;
  width: 120px;
  text-align: right;
  font-size: 15px;
  background: transparent;
  color: #1e293b;
  border: 1px solid #053676;
}

.form-input.small {
  width: 100px;
}

.form-input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.2);
}

.info-value {
  font-weight: 600;
  color: #4c1d95;
  font-size: 15px;
  white-space: nowrap;
}

.save-btn {
  background-color: #7c3aed;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
  margin-top: 20px;
}

.save-btn:hover {
  background-color: #6d28d9;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(124, 58, 237, 0.3);
}

@media (max-width: 768px) {
  .electricity-container {
    padding: 24px;
    border-radius: 0;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .form-input {
    width: 100%;
  }
}
</style>
